//
//  TTRenewalViewController.m
//  TaoTuo
//
//  Created by admin on 2019/11/13.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTRenewalViewController.h"
#import "TTGeneralTableViewCell.h"
#import "TTZFBPayTool.h"
#import "TTRenewalResultViewController.h"

static NSString *cellID = @"cell";

@interface TTRenewalViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    LLGeneralButtonCellModel *cellModel1;
    LLGeneralButtonCellModel *cellModel2;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *money;    // 选中的
@property (nonatomic, copy) NSString *year;     // 选中的

@end

@implementation TTRenewalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"续费";
    [self setupDatas];
    [self setupBottomView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(renewalSuccess:) name:dianpuRenewalSuccessNotifyKey object:nil];
}

// 店铺续费成功
- (void)renewalSuccess:(NSNotification *)notify
{
    TTRenewalResultViewController *renewalVC = [[TTRenewalResultViewController alloc] init];
    [self.navigationController pushViewController:renewalVC animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTGeneralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLGeneralButtonCellModel *model = self.dataArray[indexPath.row];
    if (model.clickBlock) {
        model.clickBlock(model, model.button);
    }
}


- (void)setupDatas
{
    [self.dataArray removeAllObjects];
    
    self.money = @"268";
    self.year = @"1";
    
    NSMutableArray *array3 = [NSMutableArray array];
    cellModel1 = [LLGeneralButtonCellModel setModelWithImageName:@"zhifubao" title:@"￥268/1年" contentAttributedString:nil rightNormalImageName:@"shopcart_unsel" rightSelectedImageName:@"zifu_sel" clickBlock:^(LLGeneralButtonCellModel * _Nullable model, UIButton * _Nullable button) {
        button.selected = YES;
        model.isSelect = YES;
        self->cellModel2.isSelect = NO;
        self.money = @"268";
        self.year = @"1";
        [self.tableView reloadData];
    } isSelect:YES];
    
    [array3 addObject:cellModel1];
    
    
    cellModel2 = [LLGeneralButtonCellModel setModelWithImageName:@"zhifubao" title:@"￥536/2年" contentAttributedString:nil rightNormalImageName:@"shopcart_unsel" rightSelectedImageName:@"zifu_sel" clickBlock:^(LLGeneralButtonCellModel * _Nullable model, UIButton * _Nullable button) {
        button.selected = YES;
        model.isSelect = YES;
        self->cellModel1.isSelect = NO;
        self.money = @"536";
        self.year = @"2";
        [self.tableView reloadData];
    } isSelect:NO];
    [array3 addObject:cellModel2];
    
    [self.dataArray addObject:cellModel1];
    [self.dataArray addObject:cellModel2];
    
    [self.tableView reloadData];
    
}

- (void)payBtnClick
{
    [TTZFBPayTool gotoOpenPlusPayWithMoney:self.money year:self.year isRenewal:YES notifyKey:dianpuRenewalSuccessNotifyKey];
}

- (void)setupBottomView
{
    UIButton *bottomBtn = [UIButton createButtonWithTitle:@"立即支付" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:UIColorHex(F2AC2F) target:self action:@selector(payBtnClick)];
    [self.view addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(55);
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 50;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.separatorColor = kSeparatorColor;
        [_tableView registerClass:[TTGeneralTableViewCell class] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(55);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
